#include <func.h>
#include "threadPool.h"
#include "virtualFileSystem.h"
#include "account.h"
#include "client.h"

int threadPoolInit(threadPool_t *pThreadPool,int workerNum){
    bzero(pThreadPool,sizeof(threadPool_t));
    pThreadPool->threadNum = workerNum;
    pThreadPool->tidArr = (pthread_t *)calloc(workerNum,sizeof(pthread_t));
    pThreadPool->taskQueue.pFront = NULL;
    pThreadPool->taskQueue.pRear = NULL;
    pThreadPool->taskQueue.size = 0;
    pThreadPool->isThreadPoolRunning = 1;
    pthread_mutex_init(&pThreadPool->mutex,NULL);
    pthread_cond_init(&pThreadPool->cond,NULL);
}

int makeWorker(threadPool_t *pThreadPool){
    for(int i=0;i<pThreadPool->threadNum;i++){
        pthread_create(pThreadPool->tidArr+i,NULL,threadFunc,(void *)pThreadPool);
    }
}
void unlock(void *parg){
    pthread_mutex_t *pMutex = (pthread_mutex_t *)parg;
    pthread_mutex_unlock(pMutex);
}

void *uploadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg;

    
    //ERROR_CHECK(ret,-1,"connect");


    puts("上传完成");
}



void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg;

    //puts(pThreadPool->taskQueue.pFront->fileName);
    while(1){
        //int sockFd = socket(AF_INET,SOCK_STREAM,0);
        task_t task;
        //char token[50] = {0};
        //char fileName[50] = {0};
        //int chose;
        pthread_mutex_lock(&pThreadPool->mutex);
        pthread_cleanup_push(unlock,&pThreadPool->mutex);
        while(pThreadPool->taskQueue.size == 0 && pThreadPool->isThreadPoolRunning == 1){
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);
        }
        if(pThreadPool->isThreadPoolRunning == 0){
            puts("child quit");
            pthread_exit(NULL);
        }
        puts("GetTask!");
        //建立新的连接

        //memcpy(&task,&pThreadPool->taskQueue.pFront,sizeof(pThreadPool->taskQueue.pFront));
        task.addr.sin_addr.s_addr = pThreadPool->taskQueue.pFront->addr.sin_addr.s_addr;
        task.addr.sin_family = pThreadPool->taskQueue.pFront->addr.sin_family;
        task.addr.sin_port = pThreadPool->taskQueue.pFront->addr.sin_port;
        //获取token
        //测试用
        //printf("pThreadPool->token: %s\n",pThreadPool->taskQueue.pFront->token);
        strcpy(task.token,pThreadPool->taskQueue.pFront->token);
        //测试用
        //printf("task->token: %s\n",task.token);
        //获取fileName
        strcpy(task.fileName,pThreadPool->taskQueue.pFront->fileName);
         //获取上传或下载的指令
        task.chose = pThreadPool->taskQueue.pFront->chose;
        //获取用户名
        strcpy(task.userName,pThreadPool->taskQueue.pFront->userName); 
        //获取登录时间
        strcpy(task.loadtime,pThreadPool->taskQueue.pFront->loadtime);
        deQueue(&pThreadPool->taskQueue);
        pthread_cleanup_pop(1);


        //服务器端功能界面代码段
        handleEvent(&task);
        //客户端断开连接，释放该线程
        close(0);
        printf("thread done! tid = %lu\n",pthread_self());
    }
}
//根据指令功选择调用
int handleEvent(task_t *task){
    int sockFd = socket(AF_INET,SOCK_STREAM,0);
    int ret = connect(sockFd,(struct sockaddr*)&task->addr,sizeof(task->addr));
    ERROR_CHECK(ret,-1,"connect");

    train_t t;

    //发送token,用于验证
    bzero(&t,sizeof(t));
    t.dataLength = strlen(task->token);
    strcpy(t.buf,task->token);
    //测试用
    //printf("t.datalength = %d\n",t.dataLength);
    //puts(task->token);
    send(sockFd,&t,sizeof(t.dataLength) + t.dataLength,0);

    //发送用户名
    bzero(&t,sizeof(t));
    t.dataLength = strlen(task->userName);
    strcpy(t.buf,task->userName);
    send(sockFd,&t,sizeof(t.dataLength) + t.dataLength,0);

    //发送登录时间
    bzero(&t,sizeof(t));
    t.dataLength = strlen(task->loadtime);
    strcpy(t.buf,task->loadtime);
    send(sockFd,&t,sizeof(t.dataLength) + t.dataLength,0);

    //接收验证结果
    int boolload;
    recv(sockFd,&boolload,sizeof(boolload),0);
    printf("boolload = %d\n",boolload);
    if(boolload == 1){
        puts("长命令接入成功！");
        //发送选择上传还是下载
        send(sockFd,&task->chose,sizeof(task->chose),0);
        //chose 4:上传
        if(task->chose == 4){
            printf("fileName: %s\n",task->fileName);
            uploadClient(sockFd,task->fileName);
        }
        //chose 5:下载
        else if(task->chose == 5){
            downClient(sockFd,task->fileName);
        }
    }
    else{
        puts("验证失败");
    }
       

}

